package com.frank.demo.service;/*
 *@author:xec
 *@date:2022/2/28  8:50
 */


import com.frank.demo.controller.TestController;
import com.frank.demo.domain.User;
import com.frank.demo.domain.UserExample;
import com.frank.demo.exception.BusinessException;
import com.frank.demo.exception.BusinessExceptionCode;
import com.frank.demo.mapper.UserMapper;
import com.frank.demo.req.UserLoginReq;
import com.frank.demo.req.UserQueryReq;
import com.frank.demo.req.UserResetPasswordReq;
import com.frank.demo.req.UserSaveReq;
import com.frank.demo.resp.UserLoginResq;
import com.frank.demo.resp.UserQueryResp;
import com.frank.demo.resp.PageResp;
import com.frank.demo.utils.CopyUtil;
import com.frank.demo.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.List;


@Service
public class UserService {
    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);

    @Resource
    private UserMapper userMapper ;

    @Resource
    private SnowFlake snowFlake;

    public List<User> getAll(){
        return userMapper.selectByExample(null);
    }

    public PageResp<UserQueryResp> getUser(UserQueryReq userQueryReq){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(userQueryReq.getLoginName())){  //如果参数不为空就带参数条件查询
            criteria.andLoginNameLike("%"+ userQueryReq.getLoginName()+"%");
        }

        PageHelper.startPage(userQueryReq.getPage(), userQueryReq.getSize());// 1代码从第几页查，3代表每页显示3条数据.
        List<User> users = userMapper.selectByExample(userExample);
        List<UserQueryResp> respList = CopyUtil.copyList(users, UserQueryResp.class);
        PageInfo<Object> pageInfo = new PageInfo<>();
        PageResp pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }

    /**
     * 更新和添加方法
     */
    public void save(UserSaveReq req) {
        User user = CopyUtil.copy(req, User.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            if(ObjectUtils.isEmpty(selectByLoginName(req.getLoginName()))){
                // 新增
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        } else {
            // 更新
//            userMapper.updateByPrimaryKey(user);
            user.setLoginName(null); // 把登录名置为空，执行更新语句的时候就不会更新登录名字段，就达到了不可以修改登录名的功能
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user); // 加上Selective的作用会判断user里面是否有内容，没有内容就不会被更新该字段
        }
    }

    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据用户名查询的方法
     * 用于校验用户名是否重复
     */
    public User selectByLoginName(String LoginName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    /**
     * 重置密码
     * @param req
     */
    public void resetPassword(UserResetPasswordReq req) {
        User user = CopyUtil.copy(req,User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }

    public UserLoginResq login(UserLoginReq req) {
        // 思考：拿用户名+密码去数据库查吗？
        // 还是先拿用户名查，然后去比对密码？
        User userDB = selectByLoginName(req.getLoginName());
        // 提示给自己看越详细越好，提示给别人看越笼统越好，防止拿数据撞库
        if(ObjectUtils.isEmpty(userDB)){
            // 用户名不存在
            LOG.info("用户名不存在，{}",req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userDB.getPassword().equals(req.getPassword())){
                // 登录成功
                UserLoginResq userLoginResq = CopyUtil.copy(userDB,UserLoginResq.class);
                return userLoginResq;
            }else{
                // 密码不正确
                LOG.info("密码不正确，输入的密码是：{}，数据库密码是：{}",req.getPassword(),userDB.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }
    }
}
